﻿using Basic.FlowApplication.Interface;
using Basic.FlowApplication.Model.Node;
using Basic.FlowCollect;
using Basic.FlowModel.AuditLogItem;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.Application.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.TimeTask
{
    internal class AuditTimeOutService : IAuditTimeOutService
    {
        private readonly IFlowAuditItemLogCollect _ItemLog;
        private readonly IFlowNodeCollect _Node;
        private readonly ISubmitAuditService _Audit;

        public AuditTimeOutService ( IFlowAuditItemLogCollect itemLog,
            IFlowNodeCollect node,
            ISubmitAuditService audit )
        {
            this._ItemLog = itemLog;
            this._Node = node;
            this._Audit = audit;
        }

        public void TimeOutHandler ()
        {
            TimeOutLogItem[] items = this._ItemLog.GetTimeOuts();
            if ( items.IsNull() )
            {
                return;
            }
            AuditNodel[] nodes = this._Node.Gets<AuditNodel>(items.ConvertAll(a => a.NodeId));
            nodes.ForEach(a =>
            {
                if ( a.TimeOutOpt == FlowTimeOutOpt.无 || a.NodeType == FlowNodeType.开始节点 )
                {
                    return;
                }
                TimeOutLogItem[] list = items.FindAll(c => c.NodeId == a.Id);
                list.ForEach(c =>
                {
                    AuditRes res = new AuditRes
                    {
                        AuditEmpId = c.EmpId,
                        ItemId = c.Id,
                        Opinion = "处理超时" + a.TimeOutOpt.ToString()
                    };
                    if ( a.TimeOutOpt == FlowTimeOutOpt.自动拒绝 )
                    {
                        res.AuditResult = FlowAuditResult.审核未通过;
                    }
                    else if ( a.TimeOutOpt == FlowTimeOutOpt.自动退回 )
                    {
                        res.AuditResult = FlowAuditResult.回退;
                        res.BackNodeId = c.PrevNodeId.Value;
                    }
                    else if ( a.TimeOutOpt == FlowTimeOutOpt.自动打回 )
                    {
                        res.AuditResult = FlowAuditResult.打回修改;
                    }
                    else if ( a.TimeOutOpt == FlowTimeOutOpt.自动转办 )
                    {
                        res.AuditResult = FlowAuditResult.转办;
                        res.ToEmpId = a.ToEmpId;
                    }
                    else if ( a.TimeOutOpt == FlowTimeOutOpt.自动委托 )
                    {
                        res.AuditResult = FlowAuditResult.委托;
                        res.ToEmpId = a.ToEmpId;
                    }
                    else if ( a.TimeOutOpt == FlowTimeOutOpt.自动向后加签 )
                    {
                        res.AuditResult = FlowAuditResult.加签;
                        res.ToEmpId = a.ToEmpId;
                        res.IsBefore = false;
                    }
                    _ = this._Audit.Submit(c.LogId, res);
                });
            });
        }
    }
}
